//
//  XHF_areasPick.m
//  省市区
//
//  Created by 徐宏飞 on 16/5/3.
//  Copyright © 2016年 徐宏飞. All rights reserved.
//

#import "XHF_areasPick.h"
#import "StateModel.h"
#import "CityModel.h"


typedef enum {
    AreaPickerStyleProvince,
    AreaPickerStyleCity,
    AreaPickerStyleArea
}AreaPickerStyle;

@interface XHF_areasPick ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *areasTableView;

@property(nonatomic, assign) AreaPickerStyle pickStyle;
@property(nonatomic, strong) NSMutableArray *provinces;
@property(nonatomic, strong) NSMutableArray *citys;
@property(nonatomic, strong) NSMutableArray *areas;
@property(nonatomic, strong) NSString *selectedProvince;//选中的省
@property(nonatomic, strong) NSString *selectedCity;//选中的市
@property(nonatomic, strong) NSString *selectedArea;//选中的区

@end

@implementation XHF_areasPick

-(NSMutableArray *)provinces{
    if (_provinces == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil];
        NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *arrayModels = [NSMutableArray array];
        for (NSMutableDictionary *dic in arrayDict) {
            StateModel *model = [StateModel modelWithDictionary:dic];
            [arrayModels addObject:model];
        }
        _provinces = arrayModels;
    }
    
    return _provinces;
}

-(void)awakeFromNib{
    [_areasTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    self.pickStyle = AreaPickerStyleProvince;

}

#pragma mark - 行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //省
    if (_pickStyle == AreaPickerStyleProvince) {
        
        return self.provinces.count;
        
    //市
    } else if (_pickStyle == AreaPickerStyleCity){
        
        return self.citys.count;
        
    //区
    } else {
        
        return self.areas.count;
    }
    
}

#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //1.取出cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    //省
    if (self.pickStyle == AreaPickerStyleProvince) {
        StateModel *model = [self.provinces objectAtIndex:indexPath.row];
        cell.textLabel.text = model.state;
    
    //市
    } else if (self.pickStyle == AreaPickerStyleCity){
        CityModel *model = [self.citys objectAtIndex:indexPath.row];
        cell.textLabel.text = model.city;
        
    //区
    }else {
        
        cell.textLabel.text = [self.areas objectAtIndex:indexPath.row];
        
    }

    //4.返回
    return cell;
}

#pragma mark - 选中执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    switch (self.pickStyle) {
        case AreaPickerStyleProvince: //省
        {
            //得到数据
            StateModel *model = [self.provinces objectAtIndex:indexPath.row];
            self.selectedProvince = model.state;
            self.citys = model.cities;
            self.pickStyle = AreaPickerStyleCity;
            [_areasTableView reloadData];
//            NSLog(@"点击的是省份 %@", self.selectedProvince);
        }
            break;
        case AreaPickerStyleCity:
        {
            CityModel *model = [self.citys objectAtIndex:indexPath.row];
            self.areas = model.areas;
            self.selectedCity = model.city;
            self.pickStyle = AreaPickerStyleArea;
            [_areasTableView reloadData];
//            NSLog(@"点击的是城市 %@", self.selectedCity);
            if (model.areas.count == 0) {
                //页面消失
                NSString *str = [NSString stringWithFormat:@"%@>%@", self.selectedProvince, self.selectedCity];
                [self.delegete disMissCurrentPageWithMessage:str];
            }
            
        }
            break;
        case AreaPickerStyleArea:
        {
           self.selectedArea = [self.areas objectAtIndex:indexPath.row];
//            NSLog(@"点击的是区 %@", self.selectedArea);
            //页面消失
            NSString *str = [NSString stringWithFormat:@"%@>%@>%@", self.selectedProvince, self.selectedCity, self.selectedArea];
            [self.delegete disMissCurrentPageWithMessage:str];
            
        }
            break;
            
            
        default:
            break;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.selectedProvince;
}

@end








